﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Text.RegularExpressions;

public partial class Admin : System.Web.UI.MasterPage
{
    clsXuLy ex = new clsXuLy();
    public static string IP="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])) || string.IsNullOrEmpty(Convert.ToString(Session["IDUser"])))
            Response.Redirect("Default.aspx");
        else
        {
            lbTaiKhoan.Text = Session["user"].ToString().ToUpper();
        }

        if (!IsPostBack)
        {
            
            LoadMenu();
            lbThongBao.Text = System.Configuration.ConfigurationSettings.AppSettings["ThongBao"].ToString().Replace("@","<br/>");
            if (IP.Trim().Equals(""))
            {
                IP = getExternalIp();
                lbIP.Text = IP;
            }
            else
                lbIP.Text = IP;
        }
    }
    private string getExternalIp()
    {
        try
        {
            string externalIP;
            externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
            externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                         .Matches(externalIP)[0].ToString();
            return externalIP;
        }
        catch { return null; }
    }
    private void LoadMenuChild(int id1, ref string kq)
    {
        DataTable dt2 = new DataTable();
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", id1);
        p[0].SqlDbType = SqlDbType.Int;
        dt2 = ex.GetData("sp_xdatblchucnang_ListChucNangChaByID", p);
        if (dt2.Rows.Count > 0)
        {
            kq += "<ul id='child'>";
            for (int j = 0; j < dt2.Rows.Count; j++)
            {
                kq += "<li><a href='" + dt2.Rows[j]["Link"].ToString() + "'>";
                kq += "<img src='css/cube_orange.gif' alt='gif' />&nbsp;&nbsp;" + dt2.Rows[j]["TenChucNang"].ToString() + "</a>";
                LoadMenuChild(int.Parse(dt2.Rows[j]["IDs"].ToString()), ref kq);
                kq += "</li>";
            }
            kq += "</ul>";
        }
    }
    private void LoadMenu()
    {
        string kq = "";
        kq += "<ul id='nav'>";
        DataTable dt = ex.GetData("sp_xdatblchucnang_ListChucNangCha");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int id1 = int.Parse(dt.Rows[i]["IDs"].ToString());
            kq += "<li class='head'><a href='" + dt.Rows[i]["Link"].ToString() + "' class='green'>";
            kq += "<img src='css/arrow.png' alt='gif' />&nbsp;&nbsp;" + dt.Rows[i]["TenChucNang"].ToString() + "</a>";

            LoadMenuChild(id1, ref kq);

            kq += "</li>";
        }
        kq += "</ul>";
        lbMenu.Text = kq;
    }

}
